package com.hub.realtime.resource;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hub.realtime.resource.domain.ResCheckpoints;
import com.hub.realtime.resource.dto.input.SavePointParam;
import com.hub.realtime.resource.service.IResCheckpointsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hub.realtime.common.annotation.Log;
import com.hub.realtime.common.core.controller.BaseController;
import com.hub.realtime.common.core.domain.AjaxResult;
import com.hub.realtime.common.enums.BusinessType;
import com.hub.realtime.resource.domain.ResSavepoint;
import com.hub.realtime.resource.service.IResSavepointService;
import com.hub.realtime.common.utils.poi.ExcelUtil;
import com.hub.realtime.common.core.page.TableDataInfo;

/**
 * 保存点管理Controller
 *
 * @author wupeng
 * @date 2022-02-07
 */
@RestController
@RequestMapping("/resource/savepoint")
public class ResSavepointController extends BaseController {
    @Autowired
    private IResSavepointService resSavepointService;

    @Autowired
    IResCheckpointsService checkpointsService;


    /**
     * 查询保存点管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:savepoint:list')")
    @GetMapping("/list")
    public TableDataInfo list(SavePointParam savePointParam) {
        Page<ResSavepoint> page = resSavepointService.list(savePointParam);
        return getDataTable(page.getRecords(),page.getTotal());
    }

    /**
     * 导出保存点管理列表
     */
    @PreAuthorize("@ss.hasPermi('resource:savepoint:export')")
    @Log(title = "保存点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SavePointParam savePointParam) {
        List<ResSavepoint> list = resSavepointService.list(savePointParam).getRecords();
        ExcelUtil<ResSavepoint> util = new ExcelUtil<ResSavepoint>(ResSavepoint.class);
        util.exportExcel(response, list, "保存点管理数据");
    }

    /**
     * 获取保存点管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('resource:savepoint:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(resSavepointService.getBaseMapper().selectById(id));
    }


    /**
     * 删除保存点管理
     */
    @PreAuthorize("@ss.hasPermi('resource:savepoint:remove')")
    @Log(title = "保存点管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(resSavepointService.deleteInfos(ids));
    }


    @GetMapping(value = "/getCheckPointByJobId/{jobId}")
    public AjaxResult getCheckPointByJobId(@PathVariable("jobId") Long jobId) {
        LambdaQueryWrapper<ResCheckpoints> checkpointsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        checkpointsLambdaQueryWrapper.eq(ResCheckpoints::getJobId, jobId);
        ResCheckpoints checkpoints = checkpointsService.getBaseMapper().selectOne(checkpointsLambdaQueryWrapper);
        if (checkpoints != null) {
            return AjaxResult.success(checkpoints);
        } else {
            return AjaxResult.success(null);
        }
    }


    @GetMapping(value = "/getSavePointByJobId/{jobId}")
    public TableDataInfo getSavePointByJobId(@PathVariable("jobId") Long jobId) {
        LambdaQueryWrapper<ResSavepoint> savepointLambdaQueryWrapper = new LambdaQueryWrapper<>();
        savepointLambdaQueryWrapper.eq(ResSavepoint::getJobId, jobId)
                .orderByDesc(ResSavepoint::getCreateTime);
        List<ResSavepoint> resSavepoints = resSavepointService.getBaseMapper().selectList(savepointLambdaQueryWrapper);
        return getDataTable(resSavepoints,null);
    }


}
